// packageA/pages/iotInstall/iotInstall.js
const app = getApp();
import CommonService from '../../../utils/service/commonService.js';
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
const util = require('../../../utils/util');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //传参
    paramObj:{},
    //信息
    info:[],
    //无数据
    showNothing: true,
    //用户权限
    authority:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      authority:app.globalData.authority
    })

    if(options.paramObj){
      let paramObj=JSON.parse(options.paramObj);

      console.log('安装信息传参');
      console.log(paramObj);

      this.setData({
        paramObj: paramObj
      })

      //获取数据
      this.getList();
    }
  },

  //获取数据
  getList(){
    Toast.loading({
      duration: 0,
      forbidClick: true,
      message: '加载中...'
    });

    let param = {
      isAuth: true,
      className: 'iotInstall',
      data: {
        searchAll: true,
        equipmentId:this.data.paramObj.equipmentId,
        selectFields: ['id','equipmentId','imei','equipmentNo','member','project','installUserObj','installTypeObj','installPositionObj','installPositionPhoto','installGroupPhoto','chatRecordPhoto','createDate']
      }
    }

    CommonService.goFindList(param).then(res => {
      Toast.clear();
      if(res.data.data && res.data.data.result && res.data.data.result.length) {
        let result=res.data.data.result[0]; 
        
        //处理日期
        if(result.createDate){
          result.createDate=util.timeFormat(result.createDate);
        }

        console.log('安装信息');
        console.log(result);

        this.setData({
          showNothing: false,
          info: result
        })
      }else{
        this.setData({
          showNothing: true
        })
      }
    }).catch((error) => {
      this.setData({
        showNothing: true
      })
    })
  },

  //跳转添加
  goToAdd(){
    let paramObj={
      equipmentId:this.data.paramObj.equipmentId
    }

    wx.navigateTo({
      url: "/packageA/pages/iotInstallAdd/iotInstallAdd?paramObj="+JSON.stringify(paramObj)
    })
  },

  //跳转编辑
  goToEdit(){
    let paramObj={
      equipmentId: this.data.paramObj.equipmentId,
      id: this.data.info.id
    }

    wx.navigateTo({
      url: "/packageA/pages/iotInstallAdd/iotInstallAdd?paramObj="+JSON.stringify(paramObj)
    })
  },

  //预览图片
  onPreviewImage(e) {
    let detail = e.target.dataset.detail;
    let src = e.target.dataset.src;
    wx.previewImage({
      current: src,
      urls: detail
    })
  }, 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  }
})